package com.huawei.zyb.taskManager.application.developer;

import com.huawei.zyb.taskManager.application.developer.dto.DeveloperDTO;
import com.huawei.zyb.taskManager.domain.developer.entity.Developer;
import com.huawei.zyb.taskManager.domain.developer.repository.DeveloperRepository;
import com.huawei.zyb.taskManager.domain.developer.valueobject.*;
import com.huawei.zyb.taskManager.domain.shared.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 开发者应用服务
 */
@Service
@Transactional
public class DeveloperApplicationService {
    
    private final DeveloperRepository developerRepository;
    
    @Autowired
    public DeveloperApplicationService(DeveloperRepository developerRepository) {
        this.developerRepository = developerRepository;
    }
    
    /**
     * 创建开发者
     */
    public DeveloperDTO createDeveloper(String id, String name, DeveloperRole role) {
        // 验证开发者ID是否已存在
        DeveloperId developerId = DeveloperId.of(id);
        if (developerRepository.existsById(developerId)) {
            throw new BusinessException("开发者ID已存在: " + id);
        }
        
        Developer developer = Developer.create(
            developerId,
            DeveloperName.of(name),
            role
        );
        
        developerRepository.save(developer);
        return convertToDTO(developer);
    }
    

    
    /**
     * 激活开发者
     */
    public void activateDeveloper(String developerId) {
        Developer developer = getDeveloperById(developerId);
        developer.setActive();
        developerRepository.save(developer);
    }
    
    /**
     * 停用开发者
     */
    public void deactivateDeveloper(String developerId) {
        Developer developer = getDeveloperById(developerId);
        developer.setInactive();
        developerRepository.save(developer);
    }
    
    /**
     * 更新开发者信息
     */
    public void updateDeveloper(String developerId, String name) {
        Developer developer = getDeveloperById(developerId);
        
        developer.updateInfo(DeveloperName.of(name));
        developerRepository.save(developer);
    }
    
    /**
     * 更改开发者角色
     */
    public void changeRole(String developerId, DeveloperRole role) {
        Developer developer = getDeveloperById(developerId);
        developer.updateRole(role);
        developerRepository.save(developer);
    }
    
    /**
     * 更新开发者状态
     */
    public void updateStatus(String developerId, DeveloperStatus status) {
        Developer developer = getDeveloperById(developerId);
        developer.updateStatus(status);
        developerRepository.save(developer);
    }
    

    
    /**
     * 删除开发者
     */
    public void deleteDeveloper(String developerId) {
        Developer developer = getDeveloperById(developerId);
        
        // 简化删除逻辑，直接删除
        developerRepository.deleteById(developer.getId());
    }
    
    /**
     * 根据ID查找开发者
     */
    @Transactional(readOnly = true)
    public DeveloperDTO findById(String developerId) {
        Developer developer = getDeveloperById(developerId);
        return convertToDTO(developer);
    }
    

    

    
    /**
     * 根据角色查找开发者列表
     */
    @Transactional(readOnly = true)
    public List<DeveloperDTO> findByRole(DeveloperRole role) {
        List<Developer> developers = developerRepository.findByRole(role);
        return developers.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    /**
     * 根据状态查找开发者列表
     */
    @Transactional(readOnly = true)
    public List<DeveloperDTO> findByStatus(DeveloperStatus status) {
        List<Developer> developers = developerRepository.findByStatus(status);
        return developers.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    /**
     * 查找所有开发者
     */
    @Transactional(readOnly = true)
    public List<DeveloperDTO> findAll() {
        List<Developer> developers = developerRepository.findAll();
        return developers.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    /**
     * 根据姓名模糊查询开发者
     */
    @Transactional(readOnly = true)
    public List<DeveloperDTO> findByNameLike(String name) {
        List<Developer> developers = developerRepository.findAll().stream()
                .filter(developer -> developer.getName().getValue().contains(name))
                .collect(Collectors.toList());
        return developers.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    /**
     * 查找可分配任务的开发者
     */
    @Transactional(readOnly = true)
    public List<DeveloperDTO> findAssignableDevelopers() {
        List<Developer> developers = developerRepository.findAll().stream()
                .filter(developer -> developer.getStatus() == DeveloperStatus.ACTIVE)
                .collect(Collectors.toList());
        return developers.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    /**
     * 查找有管理权限的开发者
     */
    @Transactional(readOnly = true)
    public List<DeveloperDTO> findManagerialDevelopers() {
        List<Developer> developers = developerRepository.findAll().stream()
                .filter(developer -> developer.getRole() == DeveloperRole.PROJECT_MANAGER || developer.getRole() == DeveloperRole.ARCHITECT)
                .collect(Collectors.toList());
        return developers.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    /**
     * 统计开发者数量
     */
    @Transactional(readOnly = true)
    public long countDevelopers() {
        return developerRepository.findAll().size();
    }
    

    
    private Developer getDeveloperById(String developerId) {
        return developerRepository.findById(DeveloperId.of(developerId))
                .orElseThrow(() -> new BusinessException("开发者不存在"));
    }
    
    private DeveloperDTO convertToDTO(Developer developer) {
        return new DeveloperDTO(
            developer.getId().getValue(),
            developer.getName().getValue(),
            developer.getRole(),
            developer.getStatus(),
            developer.getCreatedAt(),
            developer.getUpdatedAt()
        );
    }
}